<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="销售出库单号" prop="soNo" label-width="98px">
        <el-input
          v-model="queryParams.soNo"
          placeholder="请输入销售出库单号查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称/仓库编号查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户编号/名称查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.erp_sales_type" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="approver">
        <el-input
          v-model="queryParams.approver"
          placeholder="请输入审核人查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- v-loading="loading"-->
    <el-table :data="salesList">
      <el-table-column type="index" label="序号" align="center" prop="outboundNo"/>
      <el-table-column
        label="销售出库单号"
        align="center"
        prop="soNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="仓库编号"
        align="center"
        prop="warehouseId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="仓库名称"
        align="center"
        prop="warehouseName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="客户编号" align="center" prop="customerId"/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="商品种类数" align="center" prop="productType"/>
      <el-table-column label="销售结算总数额" align="center" prop="productAmount"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_sales_type" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="initiator"/>
      <el-table-column label="操作时间" align="center" prop="approveTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="approver" width="75"/>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >查看-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >提交-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改仓库信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>

      <el-form ref="form" :model="form" label-width="80px" @selection-change="handleSelectionChange">
        <el-row>
          <el-col :span="10">
            <el-form-item label="仓库名称" :span="10" prop="warehouseName">
              <el-input v-model="form.warehouseName" placeholder="请输入选择仓库"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="客户名称" :span="10" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleWarehouseQuery"
              v-hasPermi="['system:notice:add']"
            >新增
            </el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handleDelete">删除行</el-button>

          </el-col>

          <!--          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
        </el-row>
        <el-table :data="outboundList">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="序号" align="center" prop="outboundNo"/>
          <el-table-column
            label="商品编号"
            align="center"
            prop="soNo"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品名称"
            align="center"
            prop="warehouseId"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品分类"
            align="center"
            prop="warehouseName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="商品品牌" align="center" prop="customerId"/>
          <el-table-column label="可用库存数量" align="center" prop="customerName"/>
          <el-table-column label="销售出库数量" align="center" prop="productTypeCount"/>
          <el-table-column label="商品单位" align="center" prop="productTypeCount"/>
          <el-table-column label="建议销售价" align="center" prop="totalAmount"/>
          <el-table-column label="实际销售价" align="center" prop="auditStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.erp_sales_type" :value="scope.row.auditStatus"/>
            </template>
          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增选择仓库弹窗 -->
    <el-dialog :title="title1" :visible.sync="open1" width="800px" append-to-body>

      <el-form ref="form1" :model="warehouseQueryParams" label-width="80px">
        <el-row>

          <el-col :span="10">
            <el-form-item label="仓库编号" :span="10" prop="warehouseId">
              <el-input v-model="warehouseQueryParams.warehouseId" @keyup.enter.native="handleWarehouseQuery"
                        placeholder="请输入选择仓库"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="仓库名称" :span="10" prop="warehouseName">
              <el-input v-model="warehouseQueryParams.warehouseName" @keyup.enter.native="handleWarehouseQuery"
                        placeholder="请输入仓库名称"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-table class="warehouse-list" :data="warehouseList" highlight-current-row ref="singleTable"
                  @current-change="handleCurrentChange">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            label="仓库编号"
            align="center"
            prop="warehouseId"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="仓库名称"
            align="center"
            prop="warehouseName"
            :show-overflow-tooltip="true"
          />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWarehouseForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增选择客户弹窗 -->
    <el-dialog :title="title2" :visible.sync="open2" width="800px" append-to-body>

      <el-form ref="form2" :model="customerQueryParams" label-width="80px">
        <el-row>

          <el-col :span="10">
            <el-form-item label="客户编号" :span="10" prop="customerId">
              <el-input v-model="customerQueryParams.customerId" @keyup.enter.native="handleCustomerQuery"
                        placeholder="请输入客户编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="客户名称" :span="10" prop="customerName">
              <el-input v-model="customerQueryParams.customerName" @keyup.enter.native="handleCustomerQuery"
                        placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-table class="warehouse-list" :data="customerList" highlight-current-row ref="singleTable"
                  @current-change="handleCurrentChange2">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            label="客户编号"
            align="center"
            prop="customerId"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="客户名称"
            align="center"
            prop="customerName"
            :show-overflow-tooltip="true"
          />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCustomerForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增选择商品弹窗 -->
    <el-dialog :title="title3" :visible.sync="open3" width="800px" append-to-body>

      <el-form ref="form" :model="productQueryParams" label-width="80px">
        <el-row>

          <el-col :span="10">
            <el-form-item label="商品编号" :span="10" prop="productId">
              <el-input v-model="productQueryParams.productId" @keyup.enter.native="handleProductQuery" placeholder="请输入商品编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="商品名称" :span="10" prop="productName">
              <el-input v-model="productQueryParams.productName" @keyup.enter.native="handleProductQuery" placeholder="请输入商品名称"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-table class="warehouse-list" :data="productList"
                  tooltip-effect="dark" ref="multipleTable"
                  @current-change="handleSelectionChangeMore">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            label="商品编号"
            align="center"
            prop="productId"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品名称"
            align="center"
            prop="productName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品分类"
            align="center"
            prop="categoryName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品品牌"
            align="center"
            prop="brandName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="可用商品数量"
            align="center"
            prop="customerName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="商品单位"
            align="center"
            prop="unitName"
            :show-overflow-tooltip="true"
          />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCustomerForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {getSalesList} from "@/api/sales/erp_sales"
import {listCustomer} from "@/api/erp/customer"
import {listProduct} from "@/api/erp/product"
import {addWarehouse, listWarehouse, updateWarehouse} from "@/api/erp/warehouse";

export default {
  name: "",
  computed: {
    // warehouse() {
    //   return warehouse
    // }
  },
  dicts: ['erp_sales_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNo: undefined,
        warehouseName: undefined,
        customerName: undefined,
        auditStatus: undefined,
        approver: undefined,
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 列表数据
      salesList: [],
      // 表单参数
      form: {},
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 新增销售出库列表
      outboundList: [],

      // 日期范围
      dateRange1: [],
      dateRange2: [],
      rules: {
        soNo: [
          {required: true, message: "销售出库单号不能为空", trigger: "blur"}
        ],
        warehouseName: [
          {required: true, message: "仓库名称不能为空", trigger: "blur"}
        ],
        customerName: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
      },
      // 选择仓库
      form1: {
        warehouseId: null,
        warehouseName: null,
      },
      warehouseList: [],
      title1: "选择仓库",
      //   仓库筛选条件
      warehouseQueryParams: {
        pageNum: 1,
        pageSize: 10000,
        warehouseName: undefined,
        warehouseId: undefined,
      },
      currentRow: null,
      // 选择客户
      open2: false,
      customerList: [],
      customerQueryParams: {
        pageNum: 1,
        pageSize: 10000,
        customerName: undefined,
        customerId: undefined,
      },
      currentRow2: null,
      title2: "选择客户",
      form2: {
        customerId: null,
        customerName: null,
      },
      // 选择商品
      open3: false,
      productList: [],
      productQueryParams: {
        pageNum: 1,
        pageSize: 10000,
        productName: undefined,
        productId: undefined,
      },
      currentRow3: null,
      title3: "选择商品",
      form3: {
        productId: null,
        productName: null,
      },
      multipleSelection: []

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "新增销售出库单"
    },
    /** 获取列表 */
    getList() {
      this.loading = true
      getSalesList(this.queryParams).then(response => {
        console.log(1111111, response)
        this.salesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /**
     * 表单重置
     */

    reset() {
      this.form = {
        warehouseName: null,
        customerName: null,
      }
      this.form1 = {
        warehouseId: null,
        warehouseName: null,
      }
      this.resetForm("form")
      this.resetForm("form1")
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改公告"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.isActive = 1
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.open1 = false
      this.open2 = false
      this.open3 = false
      this.reset()
    },
    // 删除新增出库
    handleDelete() {

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.measureId)
      this.single = selection.length != 1
      this.multiple = !selection.length
      console.log("this.ids", this.ids)
    },
    // 新增销售出库
    handleWarehouseQuery() {
      this.open1 = true
      this.loading = true
      listWarehouse(this.warehouseQueryParams).then(res => {
        console.log(2222, res)
        this.warehouseList = res.rows
        this.loading = false
      })
    },
    // 选择仓库
    handleCurrentChange(val) {
      console.log(44444, val)
      this.currentRow = val;
      this.form1.warehouseId = val.warehouseId
      this.form1.warehouseName = val.warehouseName
    },
    // 选择仓库提交
    submitWarehouseForm() {
      if (this.form1.warehouseId) {


        this.open1 = false
        this.handleCustomerQuery()

      } else {
        this.$message.info("请先选择仓库")

      }
    },
    handleCustomerQuery() {
      this.open2 = true
      this.loading = true
      listCustomer(this.customerQueryParams).then(res => {
        console.log(3333, res)
        this.customerList = res.rows
        this.loading = false
      })
    },

    // 选择客户
    handleCurrentChange2(val) {
      console.log(44444, val)
      this.currentRow2 = val;
      this.form2.customerId = val.customerId
      this.form2.customerName = val.customerName
    }
    ,
    // 选择客户提交
    submitCustomerForm() {
      if (this.form2.customerId) {
        this.open2 = false
        this.handleProductQuery()

      } else {
        this.$message.info("请先选择客户")
      }
    },
    handleProductQuery() {
      this.open3 = true
      this.loading = true
      listProduct(this.productQueryParams).then(res => {
        console.log(5555, res, this.productQueryParams)
        this.productList = res.rows
        this.loading = false
      })
    },
    // 选择商品
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    }
    ,
    handleSelectionChangeMore(val) {
      this.multipleSelection = val;
    }

  }
}
</script>

<style scoped lang="scss">
.warehouse-list {
  height: 200px;
  overflow-y: scroll;
}
</style>
